from pygame import pypm
pypm.Initialize()
class Channel(object):
    def __init__(self, devicenum=0):
        self.devicenum = devicenum
        self.latency = 0
        self.midi_device = pypm.Output(self.devicenum, self.latency)
        
    def startnote(self, midi_note):
        self.midi_device.WriteShort(0x90, midi_note, 100)
       
    def stopnote(self, midi_note):
        self.midi_device.WriteShort(0x90, midi_note, 0)

def PrintDevices(InOrOut):
    for loop in range(pypm.CountDevices()):
        interf,name,inp,outp,opened = pypm.GetDeviceInfo(loop)
        if ((InOrOut == 0) & (inp == 1) |
            (InOrOut == 1) & (outp ==1)):
            print loop, name," ",
            if (inp == 1): print "(input) ",
            else: print "(output) ",
            if (opened == 1): print "(opened)"
            else: print "(unopened)"
    print

def prompt_for_channel():
    PrintDevices(1)
    devicenum = int(raw_input('enter devicenum: '))
    return devicenum   

if __name__ == '__main__':
    import time
    PrintDevices(1)
    devicenum = int(raw_input('enter devicenum: '))
    channel = Channel(devicenum)
    play = channel.startnote
    sleep = time.sleep
    stop = channel.stopnote
    
    play(60)
    sleep(1)
    play(65)
    sleep(1)
    stop(60)
    sleep(1)
    stop(60)
